﻿Imports System.ServiceModel

Public Class frmGetStatusInvoice

    Private Sub btnRetrive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrive.Click
        Using Comprobante = New SrvRepository.RepositorioClient()
            Dim _random As New Random()
            Dim transactionNueva As Int64 = _random.[Next]()
            Dim transactionID As Int64
            Dim token As String = String.Empty

            Dim [Error] As String
            Dim Util As New Utilerias()

            If IsNumeric(txtTransactionID.Text) Then
                transactionID = Convert.ToInt64(txtTransactionID.Text)
            End If

            [Error] = Util.CreateToken(txtRFC.Text, transactionID, token)
            If Not String.IsNullOrEmpty([Error]) Then
                ShowUnknownError([Error])
                Return
            End If
            Try
                Dim respuesta = Comprobante.EstatusComprobante(txtRFC.Text, token, transactionNueva, transactionID, txtUUID.Text)
                txtCode.Text = respuesta.Codigo.ToString()
                txtDescription.Text = respuesta.Descripcion
            Catch serviceFault As FaultException(Of SrvRepository.FallaServicio)
                Using errorForm = New frmError(TranslateFault.ToServiceError(serviceFault.Detail))
                    errorForm.ShowDialog()
                End Using
            Catch sessionFault As FaultException(Of SrvRepository.FallaSesion)
                Using errorForm = New frmError(TranslateFault.ToSessionError(sessionFault.Detail))
                    errorForm.ShowDialog()
                End Using                
            Catch ex As Exception
                ShowUnknownError(ex.Message)
            End Try
        End Using

    End Sub
    Public Sub ShowResult(ByVal message As String)
        MessageBox.Show([String].Format("{0}", message), "Ecodex UniTest")
    End Sub
    Public Sub ShowUnknownError(ByVal message As String)
        MessageBox.Show([String].Format("Error: {0}", message), "Ecodex UniTest")
    End Sub
    Public Sub ShowSessionError(ByVal [error] As Entity.SessionError)
        MessageBox.Show([String].Format("Status {0}: {1}", [error].Status.ToString(), [error].Description), "Ecodex UniTest")
    End Sub
End Class